<?php
class Default_Model_Games extends Zend_Db_Table{
	protected $_name = 'developer_files';
	protected $_primary = 'id';

	public function listGameType($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$select = $db->select()
		->from("gametype",array('id','name')) ;
		$result = $db->fetchPairs($select);


		return $result ;
	}
	public function listItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		$paginator = $arrParam['paginator'];
		$ssFilter = $arrParam['ssFilter'];
		//$db = Zend_Db::factory($adapter,$config);
		if($options['task'] == 'challenge-list'){
			$select = $db->select()
			->from('developer_files as d',array('id','name_app','link_app','link_img','star','des_app','number_download'))
			->where('d.status=1')
			->order("d.id DESC")
			;
				
				
			if($paginator['itemCountPerPage']>0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page,$rowCount);
			}
			
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('d.name_app LIKE ?',$keywords,"STRING");
			}
// 			if(!empty($ssFilter['type_app'])){
// 				$select->where('d.type_app =?',$ssFilter['type_app'],"INTEGER");
// 			}
			$result  = $db->fetchAll($select);
		}
		/*** list game ***/
		if($options['task'] == 'list-new-game'){
			$select = $db->select()
			->from('developer_files as d',array('id','name_app','link_app','link_img','star','des_app','number_download','number_play'))
			->where('d.status=?',1)
			->where('d.group_app=?',1)
			->order("d.number_play DESC")
			;
		
		
			if($paginator['itemCountPerPage']>0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page,$rowCount);
			}
			echo $ssFilter['keywords'];
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('d.name_app LIKE ?',$keywords,"STRING");
			}
			// 			if(!empty($ssFilter['type_app'])){
			// 				$select->where('d.type_app =?',$ssFilter['type_app'],"INTEGER");
			// 			}
			$result  = $db->fetchAll($select);
		}
		
		/*** list BOOK ***/
		if($options['task'] == 'list-book'){
			$select = $db->select()
			->from('developer_files as d',array('id','name_app','link_app','link_img','star','des_app','number_download','number_play'))
			->where('d.status=?',1)
			->where('d.group_app=?',2)
			->order("d.number_play DESC")
			;
		
		
			if($paginator['itemCountPerPage']>0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page,$rowCount);
			}
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('d.name_app LIKE ?',$keywords,"STRING");
			}
			// 			if(!empty($ssFilter['type_app'])){
			// 				$select->where('d.type_app =?',$ssFilter['type_app'],"INTEGER");
			// 			}
			$result  = $db->fetchAll($select);
		}
		
		
		/*** list VIDEOS ***/
		if($options['task'] == 'list-video'){
			$select = $db->select()
			->from('developer_files as d',array('id','name_app','link_app','link_img','star','des_app','number_download','number_play'))
			->where('d.status=?',1)
			->where('d.group_app=?',3)
			->order("d.number_play DESC")
			;
		
		
			if($paginator['itemCountPerPage']>0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page,$rowCount);
			}
			echo $ssFilter['keywords'];
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('d.name_app LIKE ?',$keywords,"STRING");
			}
			// 			if(!empty($ssFilter['type_app'])){
			// 				$select->where('d.type_app =?',$ssFilter['type_app'],"INTEGER");
			// 			}
			$result  = $db->fetchAll($select);
		}
		
		if($options['task']== 'article-app'){
			$select = $db->select()
			->from('developer_files as d',array('id','name_app','link_app','link_img','star','des_app','number_download','type_app'))
			->where('d.status=1')
			->where('d.id=?',$arrParam['id'],"INTEGER");
				
			$result  = $db->fetchAll($select);
		}


		return $result;

	}
	public function listItemNew($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		if($options['task'] == 'challenge-list'){
			$select = $db->select()
			->from('developer_files as d',array('id','name_app','link_app','link_img','star','des_app','number_download'))
			->where('d.status=1')
			->order("d.id DESC")
			->limit(5)
			;
			$result  = $db->fetchAll($select);
		}
		// lay danh sach cac game moi nhat
		if($options['task'] == 'list-new-game'){
			$select = $db->select()
			->from('developer_files as d',array('id','name_app','link_app','link_img','star','des_app','number_download','number_play'))
			->where('d.status=?',1)
			->where('d.group_app =?',1)
			->order("d.id DESC")
			->limit(5)
			;
			$result  = $db->fetchAll($select);
		}
		
		//  lay danh sach cac ebook moi nhat
		if($options['task'] == 'list-new-book'){
			$select = $db->select()
			->from('developer_files as d',array('id','name_app','link_app','link_img','star','des_app','number_download','number_play'))
			->where('d.status=?',1)
			->where('d.group_app =?',2)
			->order("d.id DESC")
			->limit(5)
			;
			$result  = $db->fetchAll($select);
		}
		
		//  lay danh sach cac video moi nhat
		if($options['task'] == 'list-new-video'){
			$select = $db->select()
			->from('developer_files as d',array('id','name_app','link_app','link_img','star','des_app','number_download','number_play'))
			->where('d.status=?',1)
			->where('d.group_app =?',3)
			->order("d.id DESC")
			->limit(5)
			;
			$result  = $db->fetchAll($select);
		}

		return $result;

	}
	public function countItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);

		$select = $db->select()
		->from('developer_files AS d',array('COUNT(d.id) AS totalItem'));

		// 		if(!empty($ssFilter['keywords'])){
		// 			$keywords = '%' . $ssFilter['keywords'] . '%';
		// 			$select->where('u.user_name LIKE ?',$keywords,"STRING");
		// 		}

		$result = $db->fetchOne($select);
		return $result;

	}
	
	
	// dem so luong item game
	public function countItemGame($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$ssFilter = $arrParam['ssFilter'];
		$select = $db->select()
		->from('developer_files AS d',array('COUNT(d.id) AS totalItem'))
		->where('d.group_app=?',1)
		;
		if(!empty($ssFilter['keywords'])){
			$keywords = '%' . $ssFilter['keywords'] . '%';
			$select->where('d.name_app LIKE ?',$keywords,"STRING");
		}
		// 		if(!empty($ssFilter['keywords'])){
		// 			$keywords = '%' . $ssFilter['keywords'] . '%';
		// 			$select->where('u.user_name LIKE ?',$keywords,"STRING");
		// 		}
	
		$result = $db->fetchOne($select);
		return $result;
	
	}

	
	// dem so luong item ebook
	public function countItemEbook($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$ssFilter = $arrParam['ssFilter'];
		$select = $db->select()
		->from('developer_files AS d',array('COUNT(d.id) AS totalItem'))
		->where('d.group_app=?',2)
		;
	
		// 		if(!empty($ssFilter['keywords'])){
		// 			$keywords = '%' . $ssFilter['keywords'] . '%';
		// 			$select->where('u.user_name LIKE ?',$keywords,"STRING");
		// 		}
		if(!empty($ssFilter['keywords'])){
			$keywords = '%' . $ssFilter['keywords'] . '%';
			$select->where('d.name_app LIKE ?',$keywords,"STRING");
		}
		$result = $db->fetchOne($select);
		return $result;
	
	}
	
	// dem so luong item video
	public function countItemVideo($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$ssFilter = $arrParam['ssFilter'];
		$select = $db->select()
		->from('developer_files AS d',array('COUNT(d.id) AS totalItem'))
		->where('d.group_app=?',3)
		;
		if(!empty($ssFilter['keywords'])){
			$keywords = '%' . $ssFilter['keywords'] . '%';
			$select->where('d.name_app LIKE ?',$keywords,"STRING");
		}
		$result = $db->fetchOne($select);
		return $result;
	
	}
	
	// lay danh sach ban
	public function friendsList($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		if($options == null){
			$select = $db->select()
			->from('user_group', array('id','group_name'));
			$result = $db->fetchPairs($select)	;
			$result[0] = ' -- Select an Item -- ';
			ksort($result);

		}
		return $result;
	}


	public function getTopDownload() {
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$select = $db->select()
		->from('developer_files as d',array('id','name_app','link_app','link_img','star','des_app','number_download'))
		->where('d.status=1')
		->order("d.number_download DESC")
		->limit(5)
		;
		$result  = $db->fetchAll($select);


		return $result;
	}
	public function getTopBooks() {
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$select = $db->select()
		->from('developer_files as d',array('id','name_app','link_app','link_img','star','des_app','number_download'))
		->where('d.status=1')
		->where('group_app =?',2)
		->order("d.number_play DESC")
		->limit(5)
		;
		$result  = $db->fetchAll($select);
	
		return $result;
	}
	
	public function getTopGame() {
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$select = $db->select()
		->from('developer_files as d',array('id','name_app','link_app','link_img','star','des_app','number_download'))
		->where('d.status=1')
		->where('group_app =?',1)
		->order("d.number_play DESC")
		->limit(5)
		;
		$result  = $db->fetchAll($select);
	
		return $result;
	}
	
	
	public function getTopVideo() {
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$select = $db->select()
		->from('developer_files as d',array('id','name_app','link_app','link_img','star','des_app','number_download'))
		->where('d.status=1')
		->where('group_app =?',3)
		->order("d.number_play DESC")
		->limit(5)
		;
		$result  = $db->fetchAll($select);
	
		return $result;
	}
	
	public function getTopScore() {
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$select = $db->select()
		->from('hightscore as h',array('id','user_name','user_id','score'))
		->order("h.score DESC")
		->limit(5)
		;
		$result  = $db->fetchAll($select);
	
		return $result;
	}
}